package com.test.springboot.demo;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan(basePackages = "com.test.springboot.demo.dao.game", sqlSessionFactoryRef = "factoryBean1")
@MapperScan(basePackages = "com.test.springboot.demo.dao", sqlSessionFactoryRef = "factoryBean2")
public class SpringConfig {
    @Value("${jdbc.game.url}")
    private String gameUrl;
    @Value("${jdbc.1811.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.driver}")
    private String driverClass;

    @Bean
    @Primary
    public DataSource dataSource1() {
        DriverManagerDataSource source = new DriverManagerDataSource();
        source.setUsername(username);
        source.setDriverClassName(driverClass);
        source.setUrl(gameUrl);
        source.setPassword(password);
        return source;
    }

    @Bean
    public DataSource dataSource2() {
        DriverManagerDataSource source = new DriverManagerDataSource();
        source.setUsername(username);
        source.setDriverClassName(driverClass);
        source.setUrl(url);
        source.setPassword(password);
        return source;
    }

    @Bean
    @Primary
    public SqlSessionFactory factoryBean1() {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource1());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            factoryBean.setMapperLocations(resolver.getResources("classpath*:com/test/springboot/demo/dao/mapper/game/*.xml"));
            factoryBean.setTypeAliasesPackage("com.test.springboot.demo.pojo");
            return factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Bean
    public SqlSessionFactory factoryBean2() {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource2());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            factoryBean.setMapperLocations(resolver.getResources("classpath*:com/test/springboot/demo/dao/mapper/*.xml"));
            factoryBean.setTypeAliasesPackage("com.test.springboot.demo.pojo");
            return factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
